/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sd.rmi.auction.client;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author blackdevil
 */
public class MultiClient extends Thread {

    private static int retry = 5;
    private static int step = 1;
    private final static int MAXCLIENT = 20;
    private int id;
    
    public static void main(String arg[]) {
        int numClient = (new Random().nextInt(MAXCLIENT)) ;
        System.out.println("Client number " + numClient);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            Logger.getLogger(MultiClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        for( int i =0 ; i < numClient; i++)
        {
            System.out.println("init Thread " + i);
            new MultiClient(i).start();
        }
        Thread.yield();
    }

    public MultiClient(int i)
    {
        this.id = i;
    }

    @Override
    public void run() {

        String service = "auction";
        String product;
        try {
            System.out.println("Client "+ id + " :" + "Client init");
            ClientProxy client = new ClientProxy(service);
            System.out.println("Client "+ id + " :" + "Retrieve product name");
            product = client.getProduct();
            System.out.println("Client "+ id + " :" + "Product name: " + product);
            System.out.println("Client "+ id + " :" + "Retrieve current bid");
            int currPrice = client.getCurrentBid();
            System.out.println("Client "+ id + " :" + "Current product price is" + currPrice);
            System.out.println("Client "+ id + " :" + "Try to buy: Retry number" + retry + "Step: " + step);
            for (int i = 0; i < retry; i++) {
                System.out.println("Client "+ id + " :" + "Try to offer: " + (currPrice + step));
                client.setBid(currPrice + step);
                System.out.println("Client "+ id + " :" + "Offert sent");
                currPrice = client.getCurrentBid();
                new Random().nextInt(10000);
            }
        } catch (Exception e) {
            System.err.println("Client "+ id + " :" + "Error" + e.getMessage());
            e.printStackTrace();
        }


    }
}
